﻿Public Class FormLogin
    Dim objUser As New clsUser
    Private Sub FormLogin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        connectDB()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_Warehouse_PW.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If String.IsNullOrEmpty(txtUser.Text.Trim()) = False And String.IsNullOrEmpty(txtPwd.Text.Trim()) = False Then
            Dim dtUser As DataTable = objUser.Login(txtUser.Text.Trim(), txtPwd.Text.Trim())
            If dtUser.Rows.Count > 0 Then
                g_Current_UserID = dtUser.Rows(0)("user_id")
                g_Current_UserName = dtUser.Rows(0)("user_name")
                g_Current_WorkShop = dtUser.Rows(0)("partment")
                g_Current_Classes = dtUser.Rows(0)("classes")
                Me.Hide()
                Dim main As New FormMain()
                main.ShowDialog()
                Me.Close()
            Else
                MsgBox("登录失败，错误的用户名或密码！", vbYes, "提示")
            End If
        Else
            MsgBox("请输入用户名和密码，进行登录！", vbYes, "提示")
            txtUser.Select()
        End If
    End Sub

    Private Sub txt_Click(sender As Object, e As EventArgs) Handles txtUser.Click, txtPwd.Click
        Process.Start("C:\\Program Files\\Common Files\\microsoft shared\\ink\\TabTip.exe")
        'Process.Start("C:\windows\system32\osk.exe")
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class